<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :span="6">
      <h2>欢迎来到VueAdmin管理系统</h2>
      <el-image :src="require('@/assets/erweima.jpg')" style="width: 200px ; height: 200px"></el-image>
      <p>微信号</p>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :span="6">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" style="width: 300px" type="password"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="width: 380px;">
          <el-input v-model="loginForm.code" style="width: 150px; float: left;" maxlength="5"></el-input>
          <el-image class="captchaImg" :src="captchaImg" @click="getCaptcha"></el-image>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import qs from 'qs'

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        token: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '长度 5 个字符', trigger: 'blur'}
        ],
      },
      captchaImg: null
    };
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    getCaptcha() {
      this.$axios.get('/captcha').then(res => {
        //清空用户输入的验证码
        this.loginForm.code = ''
        //获取后端接口传来的token 既生成的UUID我们根据这个值在缓存去除验证码
        this.loginForm.token = res.data.data.token
        //后端生成的验证码的BASE64编码
        this.captchaImg = res.data.data.captchaImg
      })
    },
    //登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login?' + qs.stringify(this.loginForm)).then(res => {
            //取到后端放在请求头里的jwt
            const jwt = res.headers['authorization']
            //将jwt放在store
            this.$store.dispatch('setToken', jwt)
            //跳转到首页
            this.$router.push("/index")

          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}
</script>

<style scoped>

.el-row {
  background-color: #fafafa;
  height: 740px;
  /*
    弹性布局
    垂直居中
    文字居中
   */
  display: flex;
  align-items: center;
  text-align: center;
}

.el-divider {
  height: 200px;
}

.captchaImg {
  /**
    左偏移
    左边距
    圆角
   */
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
